package com.ct.controller.minio;

import com.ct.commons.BaseController;
import com.ct.commons.annotation.PermissionApi;
import com.ct.utils.threadlocal.UserThreadLocal;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 公共服务-minio文件上传
 * @program: xxkfz-minio
 * @ClassName OSSController.java
 * @author: wust
 * @create: 2024-03-13 11:01
 * @description:
 **/
@Slf4j
@RestController
@PermissionApi(title = "文件上传模块",path="/api/v1/minio",code = "D1000",sorted = 7)
public class BaseMinioController extends BaseController {

    @Autowired
    private MinioUtils minioUtils;

    @Autowired
    private MinioConfig minioConfig;

    /**
     * 文件上传
     *
     * @param file
     */
    @PostMapping("/minio/upload")
    @PermissionApi(title = "文件上传",path="/api/v1/minio/upload",code = "D1001",sorted = 1)
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        try {
            Long systemId = UserThreadLocal.get().getSystemId();
            String path = request.getParameter("path");
            //文件名
            String fileName = file.getOriginalFilename();
            String format = new SimpleDateFormat("yyyy/MM/dd").format(new Date());
            // 重新命名文件
            String newFileName = systemId+"/"+path+"/"+format+"/"+System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
            //类型
            String contentType = file.getContentType();
            // 文件上传
            minioUtils.uploadFile(minioConfig.getBucketName(), file, newFileName, contentType);
            // 获取文件的外链
            return  minioUtils.getPresignedObjectUrl(minioConfig.getBucketName(),newFileName);
        } catch (Exception e) {
            e.printStackTrace();
            return "上传失败";
        }
    }

    /**
     * 删除
     *
     * @param fileName
     */
    @DeleteMapping("/minio/del")
    @PermissionApi(title = "删除文件上传",path="/api/v1/minio/del",code = "D1002",sorted = 2)
    public void delete(@RequestParam("fileName") String fileName) {
        minioUtils.removeFile(minioConfig.getBucketName(), fileName);
    }

    /**
     * 获取文件信息
     *
     * @param fileName
     * @return
     */
    @GetMapping("/minio/info")
    @PermissionApi(title = "获取文件信息",path="/api/v1/minio/info",code = "D1003",sorted = 3)
    public String getFileStatusInfo(@RequestParam("fileName") String fileName) {
        return minioUtils.getFileStatusInfo(minioConfig.getBucketName(), fileName);
    }

    /**
     * 获取文件外链
     *
     * @param fileName
     * @return
     */
    @GetMapping("/minio/url")
    @PermissionApi(title = "获取文件外链",path="/api/v1/minio/url",code = "D1004",sorted = 4)
    public String getPresignedObjectUrl(@RequestParam("fileName") String fileName) {
        return minioUtils.getPresignedObjectUrl(minioConfig.getBucketName(), fileName);
    }

    /**
     * 文件下载
     *
     * @param fileName
     * @param response
     */
    @GetMapping("/minio/download")
    @PermissionApi(title = "文件下载",path="/api/v1/minio/download",code = "D1005",sorted = 5)
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        try {
            InputStream fileInputStream = minioUtils.getObject(minioConfig.getBucketName(), fileName);
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/force-download");
            response.setCharacterEncoding("UTF-8");
            IOUtils.copy(fileInputStream, response.getOutputStream());
        } catch (Exception e) {
            log.error("下载失败");
        }
    }
}